<?php

namespace app\index\controller;

class Bbs extends Base
{
//前置方法
    protected $beforeActionList = [
        // 'right_hot',
        'right' => ['only' => 'index,detailed'],

    ];


    public function index($navid=null)
    {
        // 最新帖子
        $list = model('bbs')->where($this->datas)->order('sort desc,create_time deSC')->paginate();

        //最新回复帖子
        $hot = model('bbs')->where($this->datas)->order('sort desc,update_time deSC')->limit(5)->select();

        $this->assign([
            'hot'=> $hot,
            'list'=> $list,
        ]);
        return $this->fetch($this->template . $this->request->controller().'.html');
    }


    //右边最热，最新
    public function right_hot(){

        // 最新帖子
        $list = model('bbs')->where($this->datas)->order('create_time deSC')->limit(10)->select();

        //最新回复帖子
        $hot = model('bbs')->where($this->datas)->order('update_time deSC')->limit(10)->select();

        $this->assign([
            'hotten'=> $hot,
            'listten'=> $list,
        ]);
    }



    public function detailed($id)
    {
        //显示内容
        $model = $this->request->controller();

            $data = model($model)->where($this->datas)->find($id);
            model($model)->where('id',$id)->setInc('scon',1);
        if(!$data){
            $this->redirect($this->request->domain());
        }

        $list=model('bbsmass')->where($this->datas)->where('fid',$id)->limit(5)->select();
        foreach ($list as $key =>$v){
            if(empty($v['uid']))continue;
            $list[$key]['image'] = db('user')->where('id',$v['uid'])->value('image');
        }

        //更多页面
        !empty($data['cid']) ? $datas['cid'] = $data['cid'] : '';
        $datas['status'] = '1';
        $datas['lang'] = cookie('think_var');
        $next = model($model)->getnext($id, $datas);
        $this->assign([
            'data'=> $data,
            'next'=> $next,
            'list'=> $list,
            ]);
        return $this->fetch($this->template . $this->request->controller(). 'detailed.html');
    }


    //发布留言
    public function savebbs(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(!empty(session('User', '', 'User'))){
                $data['status'] = '1';
                $data['uid'] = session('User', '', 'User')->id;
                $data['username'] = session('User', '', 'User')->name;
            }else{
                $data['username'] = '匿名';
            }

            $data['lang']=cookie('think_var');
            $result = model('bbs')->allowField(true)->save($data);
            if ($result) {
                return ajax_info('提交成功', 'u');
            }
        }
    }

    //留言列表
    public function savemass(){
        $aa = session('User', '', 'User');

        if(empty($aa->id)){
//            return ajax_info('请登录再留言！');
            $data['username']='匿名';
        }else{
            $data['username']=$aa->name;
            $data['uid']=$aa->id;
        }
        if($this->request->isPost()){
            $post = $this->request->param();
            $data['fid']=$post['id'];
            $data['status']=0;
            $data['create_time']=time();

            $data['center']=$post['center'];
            $list=model('bbsmass')->save($data);
            if($list){
                model('bbs')->save(['update_time'=>time()],['id'=>$post['id']]);
                return ajax_info('留言成功','u');
            }else{
                return ajax_info('重复留言');

            }
        }
    }

    public function bbsread($id){
        $data = model('Bbs')->where($this->datas)->find($id);

        $list = model('bbsmass')->where($this->datas)->where('fid',$id)->paginate()->each(function($item, $key){
            $item['image'] =  db('user')->where('id',$item['uid'])->value('image');
            return $item;
        });
        $this->assign([
            'data'=>$data,
            'list'=>$list,
        ]);
        return $this->fetch($this->template . $this->request->action(). '.html');
    }

}

